﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/

using System;
using System.ComponentModel;
using Newtonsoft.Json;

namespace OI.DataModel
{
#if !WINDOWS_PHONE
    public class OIJurisdiction : IEditableObject, IDataErrorInfo
#else
    public class OIJurisdiction : IEditableObject
#endif
    {
        // this is the PK 
        public int ID { get; set; }

        // this is the human-readable jurisdiction identifier 
        public string JurisdictionID
        {
            get
            {
                return _JurisdictionID;
            }
            set
            {
                _JurisdictionID = value;
            }
        }
        [JsonIgnore]
        private string _JurisdictionID = "[Unnamed]";

        public string Customer { get; set; }
        public string TitleBlock { get; set; }
        public string Country { get; set; }
        public string State { get; set; }
        public string StateCode { get; set; }
        public string City { get; set; }
        public string MashupMapID { get; set; }
        public string CustomerImageURI { get; set; }


        // support undo in editors
        [JsonIgnore]
        private string Checkpoint { get; set; }

        [JsonIgnore]
        private bool InEdit { get; set; }

        [JsonIgnore]
        public Action PostCancelAction { get; set; }

        [JsonIgnore]
        public Func<OIJurisdiction, string, string> PostValidationFunction { get; set; }


        public void BeginEdit()
        {
            Checkpoint = MessageSerializer.Serialize(this);
            InEdit = true;
        }

        public void CancelEdit()
        {
            if (!string.IsNullOrEmpty(Checkpoint))
            {
                var checkpoint = MessageSerializer.Deserialize(Checkpoint) as OIJurisdiction;
                if (checkpoint != null)
                {
                    ID = checkpoint.ID;
                    JurisdictionID = checkpoint.JurisdictionID;
                    City = checkpoint.City;
                    Country = checkpoint.Country;
                    CustomerImageURI = checkpoint.CustomerImageURI;
                    MashupMapID = checkpoint.MashupMapID;
                    State = checkpoint.State;
                    StateCode = checkpoint.StateCode;
                    TitleBlock = checkpoint.TitleBlock;
                }
            }

            if (PostCancelAction != null)
            {
                PostCancelAction();
            }

            InEdit = false;
        }

        public void EndEdit()
        {
            InEdit = false;
        }

        public string Error
        {
            get { return null; }
        }


        // data validation
        public string this[string columnName]
        {
            get
            {
                // only validate when we are editing
                if (!InEdit)
                {
                    return null;
                }

                switch (columnName)
                {
                    case "JurisdictionID":
                        if (string.IsNullOrEmpty(JurisdictionID))
                        {
                            return "must be provided!";
                        }
                        break;
                    case "MashupMapID":
                        if (string.IsNullOrEmpty(MashupMapID))
                        {
                            return "must be provided!";
                        }
                        break;
                    case "StateCode":
                        if (!string.IsNullOrEmpty(StateCode) && StateCode.Length > 2)
                        {
                            return "must not exceed two characters!";
                        }
                        break;
                }

                if (PostValidationFunction != null)
                {
                    return PostValidationFunction(this, columnName);
                }
                return null;
            }
        }
    }
}